package org.grow.tiny.controller;

import org.grow.common.dto.PagedResult;
import org.grow.secure.exception.BillStateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @Author: xwg
 * @CreateDate: 2022/5/26
 */

@RestController
@RequestMapping("/tiny/common")
public class CommonController {

    @GetMapping("/show")
    public PagedResult<String> show() {
        return new PagedResult<String>(10L, Arrays.asList("NI", "Hao"));
    }

    @Autowired
    private Random random;

    @GetMapping("/calculate")
    public String calculate() {
        int nextInt = random.nextInt(100);
        return SecurityContextHolder.getContext().getAuthentication().getName() + nextInt;
    }
    @GetMapping("/throwE")
    public String throwE() {
        int nextInt = random.nextInt(100);
        System.out.println("============== "+nextInt);
        if (nextInt>10){
            throw new BillStateException("good luck "+nextInt);
        }
        return SecurityContextHolder.getContext().getAuthentication().getName() + nextInt;
    }
}
